package com.baomibing.flow.service.impl;

import com.baomibing.core.common.Assert;
import com.baomibing.flow.constant.ExecutionTypeEnum;
import com.baomibing.flow.dto.SysFlowTaskDto;
import com.baomibing.flow.entity.SysFlowTask;
import com.baomibing.flow.mapper.SysFlowTaskMapper;
import com.baomibing.flow.model.FlowUser;
import com.baomibing.flow.service.SysFlowTaskService;
import com.baomibing.orm.base.MBaseServiceImpl;
import com.baomibing.tool.util.Checker;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * SysFlowTaskServiceImpl
 *
 * @author frog 2023/10/16 10:20
 * @version 1.0.0
 **/
@Service
public class SysFlowTaskServiceImpl extends MBaseServiceImpl<SysFlowTaskMapper, SysFlowTask, SysFlowTaskDto> implements SysFlowTaskService {

    @Override
    public void saveTask(SysFlowTaskDto task) {
        saveIt(task);
    }

    @Override
    public void updateTask(SysFlowTaskDto task) {
        updateIt(task);
    }

    @Override
    public void updateTasks(List<SysFlowTaskDto> tasks) {
        updateItBatch(tasks);
    }

    @Override
    public void saveTasks(List<SysFlowTaskDto> tasks) {
        saveItBatch(tasks);
    }

    @Override
    public List<SysFlowTaskDto> listExecutingTaskByInstance(String instanceId) {
        if (Checker.beEmpty(instanceId)) {
            return Lists.newArrayList();
        }
        return mapper(baseMapper.selectList(lambdaQuery().eq(SysFlowTask::getInstanceId, instanceId).eq(SysFlowTask::getState, ExecutionTypeEnum.EXECUTING.name())));
    }

    @Override
    public List<SysFlowTaskDto> listUserExecutingTaskByUser(FlowUser user) {
        if (Checker.beNull(user) || Checker.beEmpty(user.getUserNo())) {
            return Lists.newArrayList();
        }
        return mapper(baseMapper.listUserExecutingTaskByUser(user));
    }

    @Override
    public void deleteByInstance(String instanceId) {
        Assert.CheckArgument(instanceId);
        baseMapper.delete(lambdaQuery().eq(SysFlowTask::getInstanceId, instanceId));
    }
}
